package com.zxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxy.util.Constant;
import com.zxy.mapper.*;
import com.zxy.pojo.dto.RoleDTO;
import com.zxy.pojo.dto.RoleQueryDTO;
import com.zxy.pojo.dto.SystempageDTO;
import com.zxy.pojo.dto.UserAddDTO;
import com.zxy.pojo.entity.*;
import com.zxy.pojo.vo.CourseContractResponseVO;
import com.zxy.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysPostMapper postMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Override
    public List<SysUser> getUserListByDeptId(Long deptId) {
        return sysUserMapper.selectUserListByDeptId(deptId);
    }

    //用户管理列表
    // 用户管理列表
    @Override
    public CourseContractResponseVO<SysUser> getUserList(SystempageDTO dto) {
        if (dto.getPageNum() <= 0) {
            dto.setPageNum(1);
        }
        if (dto.getPageSize() <= 0) {
            dto.setPageSize(10);
        }
        Page<SysUser> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        Page<SysUser> pages = sysUserMapper.selectUserPage(page, dto);
        CourseContractResponseVO<SysUser> responseVO = new CourseContractResponseVO<>();
        responseVO.setTotal(pages.getTotal());
        responseVO.setRows(pages.getRecords());
        responseVO.setCode(Constant.SUCCESS);
        responseVO.setMsg(Constant.MSG);
        responseVO.setParams(null);
        return responseVO;
    }

    //角色名称展示
    @Override
    public List<RoleDTO> listRoles() {
        List<SysRole> sysRoles = sysRoleMapper.selectList(null);
        log.info("得到的数据是{}:{}", sysRoles.size(), sysRoles);
        List<RoleDTO> role = new ArrayList<>();
        for (SysRole sysRole : sysRoles) {
            RoleDTO roleDTO = new RoleDTO();
            BeanUtils.copyProperties(sysRole, roleDTO);
            role.add(roleDTO);
        }
        return role;
    }

    //添加角色
    @Override
    public void addUser(UserAddDTO userDTO) {
         //根据前端传递的角色id列表给 中间表user_role添加数据
        SysUser user = new SysUser();
        BeanUtils.copyProperties(userDTO, user);
        //去部门查询dept_name
        SysDept sysDept = sysDeptMapper.selectById(userDTO.getDeptId());
        user.setDeptName(sysDept.getDeptName());
        //头像 加盐 ip 管理员用户名  暂时不上传
        user.setLoginDate(new Date());
        user.setCreateTime(new Date());
        sysUserMapper.insert(user);
        //TODO
        //下面的暂时没有数据 需要再创建一个岗位_和用户user的中间表
        List<Long> postIds = userDTO.getPostIds();
        List<Long> roleIds = userDTO.getRoleIds();
        //添加成功后会返回用户id到实体类得到这个id
        Long userId = user.getUserId();
        if (roleIds != null && !roleIds.isEmpty()) {
            for (Long roleId : roleIds) {
                //给用户和角色的中间表添加数据
                 log.info("得到的用户");
               userRoleMapper.insert(new SysUserRole(userId, roleId));
            }
        }
    }

    //查询角色
    @Override
    public CourseContractResponseVO<SysRole> getRoleList(RoleQueryDTO dto) {
        Page<SysRole> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        Page<SysRole> pages = sysRoleMapper.selectPage(page, wrapper);
        CourseContractResponseVO<SysRole> responseVO = new CourseContractResponseVO<>();
        responseVO.setTotal(pages.getTotal());
        responseVO.setRows(pages.getRecords());
        responseVO.setCode(Constant.SUCCESS);
        responseVO.setMsg(Constant.MSG);
        responseVO.setParams(null);
        return responseVO;
    }
}